/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import jpa.exceptions.NonexistentEntityException;
import model.Comment;
import model.CommentQuery;
import model.Event;
import model.EventQuery;

/**
 *
 * @author Kelompok 3
 */
public class CommentManagementLogic {

    HttpServletRequest request;

    public CommentManagementLogic(HttpServletRequest request) {
        this.request = request;
    }
    /* method untuk melakukan fungsi tambahComment, yaitu mengambil nilai textfield pada form tambahComment
     * untuk kemudian dimasukkan sebagai nilai untuk membuat baris baru pada tabel comments
     */

    public String tambahComment() {
        List<Comment> comments = new ArrayList<Comment>();
        Comment comment = new Comment();
        CommentQuery cq = new CommentQuery();
        HttpSession session = request.getSession();

        String pemberiComment = request.getParameter("pemberiComment");
        String isi = request.getParameter("isi");

        Event ename = (Event) session.getAttribute("ename");
        if (session.getAttribute("uname") == null) {
            if (pemberiComment != null) {
                comment.setIdEvent(ename);
                comment.setPemberiComment(pemberiComment);
                comment.setIsi(isi);
                session.setAttribute("tempAddComment", comment);
            } else {
                session.setAttribute("error", "masih ada field kosong");
                return "lihatEventPublic.jsp";
            }
            if (pemberiComment.equals("") || isi.equals("")) {
                session.setAttribute("error", "masih ada field kosong");
                return "lihatEventPublic.jsp";
            } else {
                cq.createComment(comment);
                comments = cq.getComments(ename);
                request.setAttribute("comments", comments);
                session.removeAttribute("tempAddComment");
                return "lihatEventPublic.jsp";
            }
        } else {
            if (pemberiComment != null) {
                comment.setIdEvent(ename);
                comment.setPemberiComment(pemberiComment);
                comment.setIsi(isi);
                session.setAttribute("tempAddComment", comment);
            } else {
                session.setAttribute("error", "masih ada field kosong");
                return "lihatEventPublic.jsp";
            }
            if (pemberiComment.equals("") || isi.equals("")) {
                session.setAttribute("error", "masih ada field kosong");
                return "lihatEvent.jsp";
            } else {
                cq.createComment(comment);
                comments = cq.getComments(ename);
                request.setAttribute("comments", comments);
                session.removeAttribute("tempAddComment");
                return "lihatEvent.jsp";
            }
        }

    }

    /* method untuk menghapus comment berdasarkan id dari comment bersangkutan
     */
    public String hapusComment() throws NonexistentEntityException {
        HttpSession session = request.getSession();
        Event event = new Event();
        List<Event> events = new ArrayList<Event>();
        EventQuery eq = new EventQuery();
        List<Comment> comments = new ArrayList<Comment>();
        Comment comment = new Comment();
        CommentQuery cq = new CommentQuery();
        if (session.getAttribute("uname") != null) {
            if (request.getParameter("idComment") != null) {
                int idComment = Integer.parseInt(request.getParameter("idComment"));
                cq.deleteComment(idComment);
                Event ename = (Event) session.getAttribute("ename");
                comments = cq.getComments(ename);
                request.setAttribute("comments", comments);
                return "lihatEvent.jsp";
            } else {
                Event ename = (Event) session.getAttribute("ename");
                comments = cq.getComments(ename);
                request.setAttribute("comments", comments);
                return "lihatEvent.jsp";
            }

        } else {
            Event ename = (Event) session.getAttribute("ename");
            comments = cq.getComments(ename);
            request.setAttribute("comments", comments);
            return "lihatEventPublic.jsp";

        }
    }
}
